<template>
  <div class="register">
    <el-select style="width: 100px; position: absolute; top: 10px; right: 20px" v-model="language" size="mini"
      placeholder="中文" @change="lang">
      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"
        :disabled="item.disabled">
      </el-option>
    </el-select>
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">{{ appTitle }}</h3>
      <el-form-item prop="username">
        <el-input v-model="registerForm.username" type="text" auto-complete="off" :placeholder="$t('user.Username')">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="registerForm.password" type="password" auto-complete="off"
          :placeholder="$t('index.Password')" @keyup.enter.native="handleRegister">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input v-model="registerForm.confirmPassword" type="password" auto-complete="off"
          :placeholder="$t('user.SureConfirmPassword')" @keyup.enter.native="handleRegister">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="role">
        <el-radio-group v-model="registerForm.role">
          <!-- <el-radio :label="0">个人</el-radio> -->
          <el-radio :label="1">{{ $t('School') }}</el-radio>
          <el-radio :label="2">{{ $t('Hospital') }}</el-radio>
          <el-radio :label="3">{{ $t('Company') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item v-if="registerForm.role > 0" prop="deviceType">
        <el-radio-group v-model="registerForm.deviceType">
          <el-radio :label="1">
            {{ $t('device.DeviceRequiredCertification') }}
          </el-radio>
          <el-radio :label="2">
            {{ $t('device.DeviceWithoutCertification') }}
          </el-radio>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item prop="code" v-if="captchaOnOff">
        <el-input v-model="registerForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
          @keyup.enter.native="handleRegister">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img" />
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
          @click.native.prevent="handleRegister">
          <span v-if="!loading">{{ $t('Register') }}</span>
          <span v-else>{{ $t('Registering') }}...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">{{$t('LoginWithExistingAccount')}}</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2022 indota.cn All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
  import {
    getCodeImg,
    register
  } from "@/api/login";
  import {
    appTitleCode
  } from '@/utils'

  export default {
    name: "Register",
    data() {
      const equalToPassword = (rule, value, callback) => {
        if (this.registerForm.password !== value) {
          callback(new Error(this.$t('user.DifferentPassword')));
        } else {
          callback();
        }
      };
      return {
        options: [{
            value: "1",
            label: "中文",
          },
          {
            value: "2",
            label: "English",
          },
        ],
        language: this.$i18n.locale === 'zh' ? "1" : '2',
        codeUrl: "",
        registerForm: {
          username: "",
          password: "",
          confirmPassword: "",
          roleId: 4,
          role: null,
          deviceType: 1,
          code: "",
          uuid: ""
        },
        registerRules: {
          username: [{
              required: true,
              trigger: "blur",
              message: this.$t('user.UsernameNull')
            },
            {
              min: 2,
              max: 20,
              message: this.$t('user.UsernameLong'),
              trigger: 'blur'
            }
          ],
          password: [{
              required: true,
              trigger: "blur",
              message: this.$t('user.PasswordNull')
            },
            {
              min: 5,
              max: 20,
              message: this.$t('user.PasswordLong'),
              trigger: 'blur'
            }
          ],
          confirmPassword: [{
              required: true,
              trigger: "blur",
              message: this.$t('user.SureConfirmPassword'),
            },
            {
              required: true,
              validator: equalToPassword,
              trigger: "blur"
            }
          ],
          role: [{
            required: true,
            trigger: "blur",
            message: this.$t('user.SelectRole')
          }],
          code: [{
            required: true,
            trigger: "change",
            message: this.$t('user.InputCaptcha')
          }]
        },
        loading: false,
        captchaOnOff: true
      };
    },
    computed: {
      appTitle() {
        const code = appTitleCode()
        return this.$t(code)
      },
    },
    created() {
      this.getCode();
    },
    methods: {
      lang(e) {
        const locale = e == 1 ? "zh" : "en"
        const vm = this
        // indexAPI.language(locale).then(() => {
        vm.$i18n.locale = locale;
        window.localStorage.setItem("lang", locale);
        Cookies.set("lang", locale)
        location.reload()
        // })
      },
      getCode() {
        getCodeImg().then(res => {
          this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
          if (this.captchaOnOff) {
            this.codeUrl = "data:image/gif;base64," + res.img;
            this.registerForm.uuid = res.uuid;
          }
        });
      },
      roleChange() {
        if (this.registerForm.role === 0) {
          this.registerForm.deviceType = 0
          return
        }
        this.registerForm.deviceType = null
      },
      handleRegister() {
        const vm = this
        this.$refs.registerForm.validate(valid => {
          if (!valid) {
            return
          }
          vm.loading = true;
          register(vm.registerForm).then(res => {
            const username = vm.registerForm.username;
            vm.$alert(vm.$t('user.RegisterSuccess', username), vm.$t('Tips'), {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              vm.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            vm.loading = false;
            if (vm.captchaOnOff) {
              vm.getCode();
            }
          })
        });
      }
    }
  };

</script>

<style rel="stylesheet/scss" lang="scss">
  .register {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    // background-image: url("/images/login-background.jpg");
    background-color: #707070;
    background-size: cover;
  }

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .register-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .el-input {
      height: 38px;

      input {
        height: 38px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .register-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .register-code {
    width: 33%;
    height: 38px;
    float: right;

    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-register-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }

  .register-code-img {
    height: 38px;
  }

</style>
